package com.fruit.controller.wap.rights;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.goods.Base;
import com.fruit.pojo.rights.UserGoodsRights;
import com.fruit.pojo.rights.UserGoodsRightsHarvest;
import com.fruit.pojo.rights.UserGoodsRightsPost;
import com.fruit.service.goods.GoodsManager;
import com.fruit.service.rights.UserRightsManager;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author joe
 */
@Controller
@RequestMapping(value = "/wap/rights")
public class UserGoodsRightControllerWap extends BaseController{
	
	@Autowired
    private UserRightsManager userRightsManager;

	@Autowired
	private GoodsManager goodsManager;
	/**
     * 获取用户权益信息详情
     *
     * @param id 权益信息ID
     * @return 权益信息详情
     */
    @RequestMapping(value = "getUserGoodsRight", method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getUserGoodsRight(@RequestParam("id") int id) {
        Map<String, Object> resMap = new HashMap<>();
        if (id <= 0) {
            resMap.put("result", this.initResult(ResultInfo.ERROR, "ERROR", "参数错误"));
        } else {
        	UserGoodsRights right = userRightsManager.getUserGoodsRight(id);
        	Base base = new Base();
        	if(right.getBaseId()!=0){
        		base = goodsManager.getBase(right.getBaseId());
        	}
        	
        	List<UserGoodsRightsHarvest> ugrsList= null;
        	List<UserGoodsRightsPost> bulletinList = null;
        	if(right.getId()!=0){
        		ugrsList = userRightsManager.findUserGoodsRightsHarvest((int) right.getId());
            	if(ugrsList!=null && ugrsList.size()!=0){
            		for(int i=0;i<ugrsList.size();i++){
            			UserGoodsRightsHarvest urh = ugrsList.get(i);
            			if(DateUtil.compareDate(urh.getExpectHarvestTime(), DateUtil.getChTime())){//如果预计收获时间大于当前时间，那么说明还没有收获，否则已经收获
            				urh.setState("01");//未收获
            			}else{
            				urh.setState("00");//已收获
            			}
                	}
            	}
            	bulletinList = userRightsManager.findUserGoodsRightsPost("00");//类型 00 权益消息气泡 01 首页消息气泡 02 钱包首页轮询消息
        	}
            resMap.put("result", this.initResult(ResultInfo.SUCCESS, "SUCCESS", "查询成功!"));
            resMap.put("rights", right);
            resMap.put("base", base);
            resMap.put("ugrsList", ugrsList);
            resMap.put("bulletinList", bulletinList);
        }
        return resMap;
    }

    
    /**
     * 获取消息
     *
     * @param id 权益信息ID
     * @return 权益信息详情
     */
    @RequestMapping(value = "getNews", method = {RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> getNews(HttpServletRequest request,HttpSession session) {
        Map<String, Object> resMap = new HashMap<>();
        //类型 00 权益消息气泡 01 首页消息气泡 02 钱包首页轮询消息
        List<UserGoodsRightsPost> bulletinList = userRightsManager.findUserGoodsRightsPost("02");
        resMap.put("bulletinList", bulletinList);
        return resMap;
    }
}
